package com.yyl.keqiance;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class ServerTest {
    public static void main(String[] args) {
        ServerSocket serverSocket = null;
        Socket accept = null;
        InputStream inputStream = null;
        //1. 创建serversocket对象并指定端口号
        try {
             serverSocket = new ServerSocket(8888);
            // 2. 等待客户端的连接 接收信息
             accept = serverSocket.accept();
            // 3. 把信息展示出来
            // 获取 输入流
             inputStream = accept.getInputStream();
            // 把输入流中的信息 转换为字符串
            // 定义字节数组
            byte[] bytes = new byte[1024];
            // 获取输入流中数据的实际长度
            int len=0;
            len=inputStream.read(bytes);
            String con=new String(bytes,0,len);
            System.out.println("客户端传过来的消息是:"+con);

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                accept.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}
